<template>
  <Modal v-model="modal" title="加发行量">
    <div>
      <Form :model="form" :label-width="100" ref="form" :rules="rules">
        <FormItem label="加发行量" prop="quantityold">
          <Input v-model="form.quantityold" v-show="false"/>
          <InputNumber class="wp40" v-model="form.quantity" precision="0" min="1" max="99999999"></InputNumber>
        </FormItem>
      </Form>
    </div>
    <div slot="footer">
      <Button @click="modal = false" size="large" type="text">取消</Button>
      <Button :loading="loading" @click="onSave" type="primary" size="large">确定</Button>
    </div>
</Modal>
</template>
<script>
export default {
  data() {
    return {
      loading: false,
      modal: false,
      btnDisabled: false,
      rules: {
        quantityold: [
          { required: true, message: '请输入库存数', trigger: "change" }
        ]
      },
      form: {
        couponId: null,
        quantity: null,
        quantityold: ''
      }
    };
  },
  methods: {
    resetForm(id) {
      this.$refs.form.resetFields();
      this.form = {
        couponId: id,
        quantity: null,
        quantityold: ''
      }
      this.loading = false
      this.openModal()
    },
    openModal() {
      this.modal = true;
    },
    closeModal() {
      this.modal = false;
    },
    onSave() {
      if (this.loading) return
      this.loading = true
      this.$refs["form"].validate(valid => {
        if (valid) {
          let param = {
            couponId: this.form.couponId,
            quantity: this.form.quantity
          }
          if (this.form.quantity < 1 || this.form.quantity > 99999999) {
            this.$Notice.error({
              title: '错误提醒',
              desc: '最小不能小于1，最大不能超过8位数字'
            })
            this.loading = false
            return
          }
          this.$ajaxPost('/coupon/coupon_info/add_amount', param).then(data => {
            this.$Notice.success({
              title: `发行量增加成功`
            })
            this.$emit('complete')
            this.closeModal()
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.loading = false
        }
      });
    }
  },
  watch: {
    'form.quantity'(v) {
      if (v == 0 || v) {
        this.form.quantityold = v + ''
      } else {
        this.form.quantityold = ''
      }
    }
  }
}
</script>

